﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestUserControl.MyControls;

namespace TestUserControl
{
    public partial class Index : System.Web.UI.Page
    {
        static MyNewControl newControl;
        static bool createAgain = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnShowUsercontrol_Click(object sender, EventArgs e)
        {
            //MyUserCotrol myControl = (MyUserCotrol)LoadControl("~/MyControls/MyUserCotrol.ascx");
            //PlaceHolder1.Controls.Add(myControl);
            newControl = LoadControl("~/MyControls/MyNewControls.ascx") as MyNewControl;
            PlaceHolder1.Controls.Add(newControl);
            Session.Add((Session.Count+1).ToString(), newControl);
            
        }

        protected void btnClickMe_Click(object sender, EventArgs e)
        {
            lblContentQuestion.Text = Session[0].ToString();
        }
        protected Control GetPostBackControl(Page page)
        {
            Control control = null;
            try
            {
                string ctrlName = page.Request.Params.Get("__EVENTTARGET");

                if (ctrlName != null && ctrlName != String.Empty)
                {
                    control = page.FindControl(ctrlName);
                }
                else
                {
                    ContentPlaceHolder cph = (ContentPlaceHolder)page.FindControl("Main");
                    for (int i = 0, len = page.Request.Form.Count; i < len; i++)
                    {
                        string[] ctl = page.Request.Form.AllKeys[i].Split('$');
                        if (ctl.Length > 2)
                        {
                            control = cph.FindControl(ctl[2]) as System.Web.UI.WebControls.Button;
                        }

                        if (control != null) break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return control;
        }
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            Control control = GetPostBackControl(this.Page);
            if ((control != null && control.ClientID == btnShowUsercontrol.ClientID) || createAgain) {
                createAgain = true;
                CreateUserControl();
            }
        }
        protected void CreateUserControl()
        {
            
            try
            {
                if (createAgain && PlaceHolder1 != null)
                {
                    if (Session.Count > 0)
                    {
                        PlaceHolder1.Controls.Clear();
                        for (int i = 0; i < Session.Count; i++)
                        {
                            switch (Session[i].ToString())
                            {
                                case "ASP.mycontrols_mynewcontrols_ascx":
                                    {
                                        // Create instance of the UserControl SimpleControl
                                        //MyNewControl ucSimpleControl = LoadControl("~/MyControls/MyNewControls.ascx") as MyNewControl;

                                        ////Add the SimpleControl to Placeholder
                                        //PlaceHolder1.Controls.Add(ucSimpleControl);
                                        lblContentQuestion.Text = Session[i].ToString();
                                        break;
                                    }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}